package com.smh.myIoTest1;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;

public class Test2 {
    public static void main(String[] args) {

        //1.定义变量
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        //2.爬取数据
        String familyNameStr = HttpUtil.get(familyNameNet);
        String boyNameStr = HttpUtil.get(boyNameNet);
        String girlNameStr = HttpUtil.get(girlNameNet);

        //正则获取数据
        ArrayList<String> familyNameTempList = (ArrayList<String>) ReUtil.findAll("(([\\u4E00-\\u9FA5]){4})(，|。)", familyNameStr, 1);
        ArrayList<String> boyNameTempList = (ArrayList<String>) ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)", boyNameStr, 1);
        ArrayList<String> girlNameTempList = (ArrayList<String>) ReUtil.findAll("(.. ){4}..", girlNameStr, 0);

        //处理数据
        //姓氏
        StringBuilder sb = new StringBuilder();
        for (String s : familyNameTempList) {
            sb.append(s);
        }
        String[] familyName = sb.toString().split("");
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyName) {
            familyNameList.add(s);
        }
        //System.out.println(familyNameList);
        //男名
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String s : boyNameTempList) {
            if (!boyNameList.contains(s)) {
                boyNameList.add(s);
            }
        }
        //System.out.println(boyNameList);
        //女名
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String s : girlNameTempList) {
            String[] split = s.split(" ");
            for (String s1 : split) {
                girlNameList.add(s1);
            }
        }
        //System.out.println(girlNameList);

        //生成数据
        ArrayList<String> infos = getInfos(familyNameList, boyNameList, girlNameList, 40, 40);
        System.out.println(infos);

        //写出数据(细节:这里写相对路径的话,相对的是class文件而言)
        FileUtil.writeLines(infos, "D:\\develop\\Code\\JavaCode\\javaclass\\day28-code\\file\\names3.txt", "UTF-8");

    }

    public static ArrayList<String> getInfos(ArrayList<String> familyNameList, ArrayList<String> boyNameList, ArrayList<String> girlNameList, int boyCount, int girlCount) {
        Random random = new Random();
        //男生名字()不重复
        HashSet<String> boyHs = new HashSet<>();
        while (true) {
            if (boyHs.size() == boyCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyHs.add(familyNameList.get(0) + boyNameList.get(0));
        }
        //女生名字(不重复)
        HashSet<String> girlHs = new HashSet<>();
        while (true) {
            if (girlHs.size() == girlCount) {
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlHs.add(familyNameList.get(0) + girlNameList.get(0));
        }
        //添加到数组中
        ArrayList<String> infos = new ArrayList<>();
        for (String boyH : boyHs) {
            infos.add(boyH + "-男-" + (random.nextInt(10) + 18));
        }
        for (String girlH : girlHs) {
            infos.add(girlH + "-女-" + (random.nextInt(8) + 18));
        }
        //ArrayList<String> infos =(ArrayList<String>)Stream.concat(boyHs.stream(), girlHs.stream()).collect(Collectors.toList());
        Collections.shuffle(infos);
        return infos;
    }
}
